<template>
	<div class="model-content">
		<div class='modal-header header'>
			<span class='title'>编辑订单</span>
			<span class='modal-cancel-btn' @click='_hideDialog'>取消</span>
		</div>
		<div class='modal-body'>
			<!--商品概要  -->
			<div class='goods-intro-container'>
				<div class='goods-synopsis'>
					<span class='good-title'>{{goodsDetails.goods_name}}</span>
					<span class='sub-goods-title'>{{goodsDetails.delivery_description?goodsDetails.delivery_description: ""}}</span>
					<span class='goods-price'>￥{{goodsDetails.selectedPrice}}</span>
				</div>
				<div class='goods-thumb-container'>
					<img class='goods-thumb' :src='goodsDetails.goods_image' />
				</div>
			</div>
			<!--商品规格  -->
			<div class='goods-specification'>
				<!--选中的样式 goods-active  -->
				<span v-for="(item,index) in goodsDetails.spec" :class='index === goodsDetails.defaultSpecIndex ? "goods-active" : ""' :key="index" @click='_changeSpec($event,item,index)' :data-spec-index="index">{{item.sp_value_name}}</span>
			</div>
			<!--选择数量  -->
			<div class='select-quantity'>
				<img class='select-num-btn' src='../../../../static/img/minus.png' @click='_minusGoodsNum' />
				<span class='selected-num'>{{goodsInfo.num}}</span>
				<img class='select-num-btn' src='../../../../static/img/plus.png' @click='_addGoodsNum' />
			</div>
		</div>
	</div>
</template>

<script>
	import { mapGetters, mapState, mapActions } from 'vuex';
	import { Toast } from 'mint-ui';
	import server from '@/server/api';
	export default {
		data() {
			return {
				goodsInfo: null,
				confirmOrderData: {},
				chooseBuyMethod: 0,
				goodsArr: [],
				params: {}
			}
		},
		computed: {
			...mapState(['goodsDetails'])
		},
		mounted() {
			if(this.goodsDetails) {
				// 设置订单的信息。默认为选中第一个小商品
				this.goodsInfo = {
					goods_id: this.goodsDetails.goods_id,
					goods_image: this.goodsDetails.goods_image,
					goods_name: this.goodsDetails.goods_name,
					goods_price: this.goodsDetails.goods_price,
					is_collocation: 1,
					num: 1,
					sp_value_id: this.goodsDetails.spec[0].sp_value_id,
					spec_name: this.goodsDetails.spec[0].sp_value_name
				}
				// 因为原本的对象中没有 selectedPrice defaultSpecIndex 相当于改变对象的长度，所以需要this.set
				// 例子：https://cn.vuejs.org/v2/guide/list.html#%E6%95%B0%E7%BB%84%E6%9B%B4%E6%96%B0%E6%A3%80%E6%B5%8B
				this.$set(this.goodsDetails, "selectedPrice", this.goodsDetails.spec[0].goods_price)
				this.$set(this.goodsDetails, "defaultSpecIndex", 0)
				this.$set(this.goodsDetails, "totalPrice", this.goodsDetails.spec[0].goods_price)
				
			}
		},
		created() {},
		methods: {
			...mapActions(['getShoppingList', 'getSelectGoods']),
			_hideDialog() {
				var param = {
					isShowModel:false,
					chooseBuyMethod:null,
					optionModelName:null
				}
				this.$emit("showModel", param)
			},
			_addGoodsNum() {
				if(this.goodsInfo) {
					this.goodsInfo.num++;
					this.$set(this.goodsDetails, "totalPrice", this.goodsInfo.num * this.goodsDetails.selectedPrice)
				}
			},
			_minusGoodsNum() {
				if(this.goodsInfo.num <= 1) {
					Toast("不能再减了！！！");
					return;
				}
				if(this.goodsInfo) {
					this.goodsInfo.num--;
					this.$set(this.goodsDetails, "totalPrice", this.goodsInfo.num * this.goodsDetails.selectedPrice)
				}
			},
			_changeSpec(event, item, index) {
				// 改变小商品信息
				this.$set(this.goodsDetails, "selectedPrice", this.goodsDetails.spec[index].goods_price)
				this.$set(this.goodsDetails, "defaultSpecIndex", index)
				this.$set(this.goodsDetails, "totalPrice", this.goodsInfo.num * this.goodsDetails.selectedPrice)
				this.goodsInfo.sp_value_id = this.goodsDetails.spec[index].sp_value_id;
				this.goodsInfo.spec_name = this.goodsDetails.spec[index].sp_value_name;
				this.goodsInfo.goods_price = this.goodsDetails.selectedPrice;
			}
		}

	}
</script>

<style scoped lang="scss">
	.modal-content {
		width: calc(100% - 60px);
		margin: 0 30px;
		background-color: #fff;
		border-radius: 8px;
		.modal-header {
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 20px 40px;
			.title {
				line-height: 56px;
				font-size: 40px;
				color: #4A4A4A;
			}
			.modal-cancel-btn {
				color: #ef364e;
				font-size: 32px;
			}
		}
		.modal-body {
			padding: 0 20px;
			max-height: 800px;
			overflow-y: auto;
			.goods-intro-container {
				display: flex;
				justify-content: space-between;
				padding-top: 40px;
				margin-bottom: 60px;
				border-top: 1px solid #dcdcdc;
				.goods-synopsis {
					display: flex;
					flex-direction: column;
					width: 430px;
					.good-title {
						overflow: hidden;
						height: 50px;
						width: 430px;
						margin-bottom: 5px;
						line-height: 50px;
						text-overflow: ellipsis;
						white-space: nowrap;
						color: #4A4A4A;
						font-size: 36px;
					}
					.sub-goods-title {
						margin-bottom: 30px;
						font-size: 28px;
						color: #9b9b9b;
						line-height: 40px;
					}
					.goods-price {
						font-size: 36px;
					}
				}
				.goods-thumb-container {
					width: 170px;
					height: 144px;
					flex-shrink: 0;
					img {
						width: 100%;
						height: 100%;
					}
				}
			}
			.goods-specification {
				display: flex;
				flex-wrap: wrap;
				padding-bottom: 40px;
				.goods-active {
					background-color: #F8F8F8;
				}
				span {
					padding: 20px 30px;
					margin-right: 20px;
					margin-bottom: 20px;
					border: 2px solid #e6e6e6;
					border-radius: 8px;
					color: #67666b;
					font-size: 28px;
				}
			}
			.select-quantity {
				display: flex;
				align-items: center;
				padding-bottom: 60px;
				border-bottom: 2px solid #DCDCDC;
				.select-num-btn {
					width: 100px;
					height: 60px;
				}
				.selected-num {
					min-width: 30px;
					padding: 0 30px;
					text-align: center;
					font-size: 36px;
					color: #000;
				}
			}
		}
	}
</style>